package com.yilijk.base.utils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yilijk.base.constants.UserInfo;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.config.BaseUrl;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class ClickUtils {

    //    解决快速点击事件
   public static   void setFastOnClickListener(View view, View.OnClickListener onClickListener){
        if (view == null)return;
        view.setOnClickListener(view1 -> {
            onClickListener.onClick(view);
            //设置埋点网络请求
            view1.setClickable(false);
            view.postDelayed(() -> {
                view.setClickable(true);
            }, 500);
        });
    }

    public static void setAdapterItemFastOnClickListener(BaseQuickAdapter adapter, BaseQuickAdapter.OnItemClickListener onItemClickListener){
        if (adapter == null) return;
        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            onItemClickListener.onItemClick(adapter1,view1,position);
            //设置埋点网络请求
            view1.setClickable(false);
            view1.postDelayed(() ->{
                view1.setClickable(true);
            },500);
        });
    }
    public static  void setAdapterItemChildFastOnClickListener(BaseQuickAdapter adapter, BaseQuickAdapter.OnItemChildClickListener onItemChildClickListener){
        if (adapter == null) return;
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            onItemChildClickListener.onItemChildClick(adapter,view,position);
            //设置埋点网络请求
            view.setClickable(false);
            view.postDelayed(() ->{
                view.setClickable(true);
            },500);
        });
    }

    public static void sendEvent(String actionID, Context context) {
        sendEvent(actionID, context, false, new JSONObject());
    }

    public static void sendEvent(String actionID, Context context,String desc){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name","android_"+desc);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        sendEvent(actionID,context,false,jsonObject);
    }

    public static void sendEvent(String actionID, Context context, boolean isRn, JSONObject jsonObject){
       if (!OpenUtils.getInstance().isOpenSendEvent())return;
       try {
           StringBuilder pageName = new StringBuilder("");
               ALog.e("event：","isRn: "+ isRn + " " + (context instanceof  Activity));
           if (isRn && context instanceof Activity) {
               Intent intent = ((Activity) context).getIntent();
               pageName.append(intent.getStringExtra("intent_key_plugin_id"));
               pageName.append("_").append(intent.getStringExtra("intent_key_module_id"));
               pageName.append("_").append(intent.getStringExtra("intent_key_component_name"));
           }
           jsonObject.put("page_name",isRn?pageName.toString():context.getClass().getSimpleName());
           jsonObject.put("eventId", actionID);
           jsonObject.put("action",actionID.endsWith("_load")?"load":"event");
           jsonObject.put("time",System.currentTimeMillis());
           jsonObject.put("guestid",DevicesUtils.getDeviceNo());
           jsonObject.put("deviceInfo",
                   "brand:"+"APP"+
                           ",model:"+ DevicesUtils.getDeviceBrand()+" "+DevicesUtils.getDeviceName()+" "+DevicesUtils.getDeviceId()+
                           " system:"+DevicesUtils.getSystemVersion()+
                           ",platform:"+"APP");
           jsonObject.put("visibleid","");
           jsonObject.put("page_id","");
           jsonObject.put("refer",!TextUtils.isEmpty(ActivityManagerUtils.referPage)? ActivityManagerUtils.referPage :"");
           jsonObject.put("pre_page_id","");
           jsonObject.put("environment", HttpUtils.getEnvironmentNo() == 4 ? "prod" : "local");
           jsonObject.put("version","");
           jsonObject.put("channel",DevicesUtils.getTerminal());
           jsonObject.put("appVersion", DevicesUtils.getVersionName());
           jsonObject.put("wxVersion", "");
           jsonObject.put("SDKVersion", "");
           jsonObject.put("scene","");
           jsonObject.put("domain","https://api.yilijk.com");

//           NetWorkUtils.initNetWorkUtils(context).get(ConfigureConstants.dominNo == 4?"https://stat.yilijk.com/nut/stat":"https://api.d1.yilijk.com/cobra-collector/nut/stat", jsonObjectParams, new NetWorkCallBack() {
           HttpUtils.post(HttpUtils.getPointUrl(),OpenUtils.getInstance().getClickParams(jsonObject), new HttpCallBack<String>() {
               @Override
               public void onSuccess(String result, int code) {
                   ALog.i("ClickUtils","埋点成功==="+code+"-"+actionID);
               }

               @Override
               public void onFailure(String failureMsg, int statusCode) {
                   ALog.i("ClickUtils","埋点失败==="+statusCode+"-"+actionID+failureMsg);

               }
           });
       }catch (Exception e){

       }


    }

}
